package com.uzai.console.component;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.console.dto.log.userloginlog.UserLoginLogQuery;
import com.uzai.console.dto.log.useroperationlog.UserOperationLogQuery;
import com.uzai.console.entity.es.EsUserLoginLog;
import com.uzai.console.service.ip138.Ip138ApiService;
import com.uzai.console.service.log.UserLoginLogService;
import com.uzai.console.service.log.UserOperationLogService;
import com.uzai.console.vo.ip138.Ip138Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取IP138.comip地址工具类
 * @author jianghan
 * @date 2017/1/10
 */
@Component
public class Ip138InfoUtil {

    @Autowired
    private Ip138ApiService ip138ApiService;
    @Autowired
    private UserLoginLogService userLoginLogService;

    /**
     * 获取请求用户的本地IP地址信息
     * @return
     */
    public Ip138Info getRequestIpInfo() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return getRequestIpInfo(request);
    }

    /**
     * 获取请求用户的IP地址信息对象
     * @param request
     * @return
     */
    private Ip138Info getRequestIpInfo(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");

        //ip = "113.87.193.54";

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if ("0:0:0:0:0:0:0:1".equals(ip) || "127.0.0.1".equals(ip)) {
            Ip138Info ip138Info = new Ip138Info();
            ip138Info.setIp("127.0.0.1");
            ip138Info.setCountry("中国");
            ip138Info.setProvince("局域网IP");
            return ip138Info;
        }else if(ip.startsWith("192.168")){
            Ip138Info ip138Info = new Ip138Info();
            ip138Info.setIp(ip);
            ip138Info.setCountry("中国");
            ip138Info.setProvince("局域网IP");
            return ip138Info;
        }else{


            //判断ip在数据库当中是否存在，如果存在，则不需要去请求ip138接口解析IP信息
            UserLoginLogQuery userLoginLogQuery = new UserLoginLogQuery();
            userLoginLogQuery.setIp(ip);
            userLoginLogQuery.setLoginStatus(1);//登录成功
            Page<EsUserLoginLog> userLoginLogPage = userLoginLogService.findUserLoginLogList(userLoginLogQuery);
            if(userLoginLogPage != null && userLoginLogPage.getRecords() != null && userLoginLogPage.getRecords().size() > 0){//此ip在ES中存在，则直接从ES中获取IP信息
                EsUserLoginLog esUserLoginLog = userLoginLogPage.getRecords().get(0);
                Ip138Info ip18Info = new Ip138Info();
                ip18Info.setIp(ip);
                ip18Info.setCountry(esUserLoginLog.getCountry());
                ip18Info.setProvince(esUserLoginLog.getProvince());
                ip18Info.setCity(esUserLoginLog.getCity());
                ip18Info.setArea(esUserLoginLog.getArea());
                return ip18Info;
            }else{//则从IP138.com中解析ip信息
                return ip138ApiService.findIpInfo(ip);
            }
        }
    }

}
